package org.chenliang.emulator.ui;

import java.util.Random;

import org.cllib.tool.SWTUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

public class MainWindow implements Runnable, TextResource{
	Display display;
	Shell shell;

	Image icon;
	Canvas c;
	
	StatusBar statusBar;
	
	private final static String ICON_APPLICATION = "/emulator.png";
	
	SystemSettingManager ssm = SystemSettingManager.getInstance();
	
	Random r = new Random();
	public MainWindow() {
		new Thread(this).start();
	}
	
	public void run() {
		display = new Display();

		icon = new Image(display, this.getClass().getResourceAsStream(ICON_APPLICATION));
		
		display.addFilter(SWT.KeyDown, new Listener() {

			public void handleEvent(Event event) {
				c.redraw();
			}

		});
		
		display.addFilter(SWT.KeyUp, new Listener() {

			public void handleEvent(Event event) {
				
				c.redraw();
			}

		});
		
		shell = new Shell(display , SWT.TITLE | SWT.CLOSE | SWT.MIN);
		
		shell.setImage(icon);
		shell.setText(TEXT_APPLICATION_TITLE);

		shell.addShellListener(new ShellAdapter() {

			@Override
			public void shellClosed(ShellEvent e) {
				super.shellClosed(e);
			}

		});

		shell.setLayout(new FormLayout());

		initializeData();
		
		initializeGUI();

		shell.layout();
		
		final Rectangle r = shell.computeTrim(50, 50, ssm.getScreenWidth(), ssm.getScreenHeight() + statusBar.getBounds().height);
		shell.setBounds(r);
		
		shell.open();
		
		afterStartUp();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

		icon.dispose();
		display.dispose();
	}
	
	private void afterStartUp() {
		c.redraw();
	}
	

	private int showMessage(String title, String message, int style) {
		MessageBox mb = new MessageBox(shell, style);
		mb.setText(title);
		mb.setMessage(message);
		return mb.open();
	}
	
	
	private void buildFileMenu(Menu menu) {
		SWTUtil.disposeMenuItems(menu);

		MenuItem openProjectItem = new MenuItem(menu, SWT.PUSH);
		openProjectItem.setText(TEXT_MENU_FILE_OPEN + "\tCtrl+O");
		openProjectItem.setAccelerator(SWT.MOD1 + 'o');

		openProjectItem.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				super.widgetSelected(e);
				load();
			}

		});

		new MenuItem(menu, SWT.SEPARATOR);

		MenuItem exitItem = new MenuItem(menu, SWT.PUSH);
		exitItem.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				super.widgetSelected(e);
				shell.close();
			}

		});
		exitItem.setText(TEXT_MENU_FILE_EXIT);
	}
	
	private void buildSettingMenu(Menu menu){
		SWTUtil.disposeMenuItems(menu);
		
		
	}
	
	private void openAboutDialog() {
		AboutDialog aboutDialog = new AboutDialog(shell);
		aboutDialog.open();
	}
	
	private void buildHelpMenu(Menu menu) {
		SWTUtil.disposeMenuItems(menu);
		
		MenuItem aboutItem = new MenuItem(menu, SWT.PUSH);
		aboutItem.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				super.widgetSelected(e);
				openAboutDialog();
			}

		});
		aboutItem.setText(TEXT_MENU_HELP_ABOUT + "\tF1");
		aboutItem.setAccelerator(SWT.F1);
	}
	
	private void createMenu(){
		Menu menuBar = new Menu(shell, SWT.BAR);

		// file menu
		MenuItem menuBarFileItem = new MenuItem(menuBar, SWT.CASCADE);
		menuBarFileItem.setText(TEXT_MENU_FILE + "(&F)");

		final Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);

		fileMenu.addMenuListener(new MenuAdapter() {

			@Override
			public void menuShown(MenuEvent e) {
				super.menuShown(e);
				buildFileMenu(fileMenu);
			}

		});

		buildFileMenu(fileMenu);

		menuBarFileItem.setMenu(fileMenu);

		MenuItem menuBarSettingItem = new MenuItem(menuBar, SWT.CASCADE);
		menuBarSettingItem.setText(TEXT_MENU_SETTING + "(&S)");
		
		final Menu settingMenu = new Menu(shell, SWT.DROP_DOWN);

		settingMenu.addMenuListener(new MenuAdapter() {

			@Override
			public void menuShown(MenuEvent e) {
				super.menuShown(e);
				buildSettingMenu(fileMenu);
			}

		});

		buildSettingMenu(settingMenu);
		
		menuBarSettingItem.setMenu(settingMenu);
		
		MenuItem menuBarToolItem = new MenuItem(menuBar, SWT.CASCADE);
		menuBarToolItem.setText(TEXT_MENU_TOOL + "(&T)");
		
		final Menu toolMenu = new Menu(shell, SWT.DROP_DOWN);

		toolMenu.addMenuListener(new MenuAdapter() {

			@Override
			public void menuShown(MenuEvent e) {
				super.menuShown(e);
				buildSettingMenu(fileMenu);
			}

		});

		buildSettingMenu(toolMenu);
		
		menuBarSettingItem.setMenu(toolMenu);
		
		MenuItem menuBarHelpItem = new MenuItem(menuBar, SWT.CASCADE);
		menuBarHelpItem.setText(TEXT_MENU_HELP + "(&H)");

		Menu helpMenu = new Menu(menuBar);

		buildHelpMenu(helpMenu);

		menuBarHelpItem.setMenu(helpMenu);

		shell.setMenuBar(menuBar);
	}
	
	private void createToolBar(){
		
	}
	
	private Composite createContent(){
//		FormData layoutData = null;
//		
//		layoutData = new FormData();
//		layoutData.left = new FormAttachment(0, 0);
//		layoutData.top = new FormAttachment(0, 0);
//		layoutData.right = new FormAttachment(100, 0);
//		layoutData.bottom = new FormAttachment(100 , 0);
		
		c = new Canvas(shell , SWT.DOUBLE_BUFFERED | SWT.NO_BACKGROUND);
		
//		c.setLayoutData(layoutData);
		
		c.addPaintListener(new PaintListener(){

			public void paintControl(PaintEvent e) {
				// TODO Auto-generated method stub
				final GC gc = e.gc;
				
				final Rectangle cb = c.getBounds();
				final Point cs = c.getSize();
				
				final Image buffer = new Image(display , cs.x , cs.y);
				final GC bggc = new GC(buffer);
				
				//draw everything to bgc , bgc.drawXXX , bgc.fillXXX
				final Color bgc = new Color(display , r.nextInt(255) ,r.nextInt(255) , r.nextInt(255));
				
				bggc.setBackground(bgc);
				bggc.fillRectangle(0 , 0 , cs.x , cs.y);
				//done with the buffer
				
//				gc.setClipping(0 , 0 , cs.x , cs.y);
				gc.drawImage(buffer, 0, 0);
				
				buffer.dispose();
				bggc.dispose();
				bgc.dispose();
			}
			
		});
		
//		c.setSize(ssm.getScreenWidth(), ssm.getScreenHeight());
		
		return c;
	}
	
	private StatusBar createStatusBar(){
		StatusBar statusBar = new StatusBar(shell , SWT.None , 1);
		return statusBar;
	}
	
	private void initializeGUI() {
		FormData layoutData = null;
		
		layoutData = new FormData();
		layoutData.left = new FormAttachment(0, 0);
		layoutData.top = new FormAttachment(0, 0);
		layoutData.right = new FormAttachment(100, 0);
		layoutData.bottom = new FormAttachment(100 , 0);
		
		createMenu();
		
		createToolBar();
		
		Composite c = createContent();
		
		statusBar = createStatusBar();
		
		layoutData = new FormData();
		layoutData.left = new FormAttachment(0, 0);
		layoutData.top = new FormAttachment(0, 0);
		layoutData.right = new FormAttachment(100, 0);
		layoutData.bottom = new FormAttachment(statusBar, 0 , SWT.TOP);
		
		c.setLayoutData(layoutData);
		
		layoutData = new FormData();
		layoutData.left = new FormAttachment(0, 0);
//		layoutData.top = new FormAttachment(0, 0);
		layoutData.right = new FormAttachment(100, 0);
		layoutData.bottom = new FormAttachment(100 , 0);
		
		statusBar.setLayoutData(layoutData);
	}
	
	private void save(){
		
	}
	
	private void updateStatus(String s){
		statusBar.setText(0, s);
	}
	
	private void load(){
		DirectoryDialog dg = new DirectoryDialog(shell);
	}
	
	private void initializeData(){
	}
}

